package com.ymatou.iapi.productmanager.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.GetSpecificationsByUserBean;
import com.ymatou.iapi.productmanager.service.GetSpecificationsByUserCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.compare.AssertHelper;


public class Ts_GetSpecificationsByUser {
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App使用规格属性查询");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetSpecificationsByUserCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSpecificationsByUser_001() {
		Logger.start(true, "App使用规格属性查询");
		try{
			JSONObject ret = new GetSpecificationsByUserCall().call(sellerId);
			checkGetSpecificationsByUser(sellerId,ret);
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSpecificationsByUser_002() {
		Logger.start(true, "不存在sellerId");
		try{
			JSONObject ret = new GetSpecificationsByUserCall().call(123);
			checkGetSpecificationsByUser(123,ret);
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSpecificationsByUser_003() {
		Logger.start(true, "sellerId=0");
		try{
			JSONObject ret = new GetSpecificationsByUserCall().call(0);
			Logger.verifyEquals(400, ret.getInt("Code"), "return code");
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetSpecificationsByUser_004() {
		Logger.start(true, "sellerId=-1");
		try{
			JSONObject ret = new GetSpecificationsByUserCall().call(-1);
			Logger.verifyEquals(400, ret.getInt("Code"), "return code");
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	
	public static void checkGetSpecificationsByUser(int userId,JSONObject ret) throws JSONException{
		Logger.verifyEquals(200, ret.getInt("Code"), "return code");
		GetSpecificationsByUserBean actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"),GetSpecificationsByUserBean.class);
		GetSpecificationsByUserBean tgtBean = getGetSpecificationsByUserBeanFromSql(userId);
		AssertHelper.assertResultEqual(tgtBean, actBean, "GetSpecificationsByUser");
		
	}
	
	
	public static GetSpecificationsByUserBean getGetSpecificationsByUserBeanFromSql(int userId){
		List<String> propertyList = intergratedProductMapper.selectSpecificationsByUserId(userId).stream()
								.map(x->x.get("sProductProperty").toString()).collect(Collectors.toList());
		
		GetSpecificationsByUserBean bean  =new GetSpecificationsByUserBean();
		bean.setNames(propertyList);
		return bean;
	}
	

}
